home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicMenuUI.java < prev    next >
Text File  |  1998-06-30  |  30KB  |  713 lines

  1. /*
  2.  * @(#)BasicMenuUI.java    1.94 98/06/22
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package com.sun.java.swing.plaf.basic;
  16.  
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import java.beans.*;
  20. import com.sun.java.swing.*;
  21. import com.sun.java.swing.event.*;
  22. import com.sun.java.swing.plaf.*;
  23. import com.sun.java.swing.border.*;
  24. import java.io.Serializable;
  25.  
  26.  
  27. /**
  28.  * A Windows L&F implementation of MenuUI.  This implementation 
  29.  * is a "combined" view/controller.
  30.  * <p>
  31.  * Warning: serialized objects of this class will not be compatible with
  32.  * future swing releases.  The current serialization support is appropriate 
  33.  * for short term storage or RMI between Swing1.0 applications.  It will
  34.  * not be possible to load serialized Swing1.0 objects with future releases
  35.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  36.  * baseline for the serialized form of Swing objects.
  37.  *
  38.  * @version 1.94 06/22/98
  39.  * @author Georges Saab
  40.  * @author David Karlton
  41.  * @author Arnaud Weber
  42.  */
  43. public class BasicMenuUI extends MenuUI implements Serializable 
  44. {
  45.     protected JMenu                  menu;
  46.     protected MouseListener          mouseListener;
  47.     protected MouseMotionListener    dragListener;
  48.     protected ChangeListener         menuChangeListener;
  49.     protected PropertyChangeListener propertyChangeListener;
  50.  
  51.     protected static Color pressedBackground;
  52.     protected static Color pressedForeground;
  53.     private int lastMnemonic = 0;
  54.  
  55.     // visual constants
  56.     protected static final int defaultTextIconGap = 4;
  57.  
  58.     protected Icon menuArrow = null;
  59.     protected Icon checkIcon = null;
  60.  
  61.     protected boolean oldBorderPainted;
  62.  
  63.     public static ComponentUI createUI(JComponent x) {
  64.     return new BasicMenuUI();
  65.     }
  66.  
  67.     public void installUI(JComponent c) {
  68.     menu = (JMenu) c;
  69.     // Create and install listeners
  70.     menu.setDelay(200);
  71.     initListeners(c);
  72.     addListeners(c);
  73.     // Set defaults
  74.     c.setOpaque(true);
  75.     LookAndFeel.installBorder(c,"Menu.border");
  76.     oldBorderPainted = menu.isBorderPainted();
  77.     menu.setBorderPainted( ( (Boolean) (UIManager.get("MenuItem.borderPainted")) ).booleanValue() );
  78.     LookAndFeel.installColorsAndFont(c,
  79.                           "Menu.background",
  80.                           "Menu.foreground",
  81.                           "Menu.font");
  82.         validateKeyboardAccelerator();
  83.  
  84.     // Menu specific defaults
  85.     if (pressedBackground == null || 
  86.         pressedBackground instanceof UIResource) {
  87.         pressedBackground = 
  88.         UIManager.getColor("Menu.pressedBackground");
  89.     }
  90.     if (pressedForeground == null || 
  91.         pressedForeground instanceof UIResource) {
  92.         pressedForeground = 
  93.         UIManager.getColor("Menu.pressedForeground");
  94.     }
  95.     }
  96.     
  97.     public void uninstallUI(JComponent c) {
  98.     menu.setArmed(false);
  99.     menu.setSelected(false);
  100.     removeListeners(c);
  101.     c.resetKeyboardActions();
  102.     LookAndFeel.uninstallBorder(c);    
  103.     ((JMenu) c).setBorderPainted( oldBorderPainted );
  104.     if (menuArrow instanceof UIResource)
  105.         menuArrow = null;
  106.     if (checkIcon instanceof UIResource)
  107.         checkIcon = null;
  108.     }
  109.  
  110.     protected void validateKeyboardAccelerator() {
  111.         if(menu.getModel().getMnemonic() != lastMnemonic) {
  112.             menu.unregisterKeyboardAction(KeyStroke.getKeyStroke(lastMnemonic,ActionEvent.ALT_MASK,false));
  113.             lastMnemonic = menu.getModel().getMnemonic();
  114.             menu.registerKeyboardAction(new PostAction(menu,true),            
  115.                                         KeyStroke.getKeyStroke(lastMnemonic,ActionEvent.ALT_MASK,false),
  116.                                         JComponent.WHEN_IN_FOCUSED_WINDOW);
  117.         } 
  118.     }
  119.  
  120.     protected MouseListener createMouseListener(JComponent c) {
  121.     return new MouseListener((JMenu)c);
  122.     }
  123.  
  124.     protected MouseMotionListener createMouseMotionListener(JComponent c) {
  125.         return new BasicMenuMouseMotionListener();
  126.     }
  127.  
  128.     protected ChangeListener createMenuChangeListener(JComponent c) {
  129.         return new MenuChangeListener((JMenu)c, this);
  130.     }
  131.  
  132.     protected PropertyChangeListener createPropertyChangeListener(JComponent c) {
  133.         return new MenuPropertyChangeListener();
  134.     }
  135.     
  136.     protected void initListeners(JComponent c) {
  137.         mouseListener = createMouseListener(c);
  138.         dragListener  = createMouseMotionListener(c);
  139.         menuChangeListener = createMenuChangeListener(c);
  140.         propertyChangeListener = createPropertyChangeListener(c);
  141.     }
  142.  
  143.     protected void addListeners(JComponent c) {
  144.         c.addMouseListener(mouseListener);
  145.         c.addMouseMotionListener(dragListener);
  146.         ((JMenu)c).addChangeListener(menuChangeListener);
  147.         ((JMenu)c).addPropertyChangeListener(propertyChangeListener);
  148.     }
  149.  
  150.     protected void removeListeners(JComponent c) {
  151.         c.removeMouseListener(mouseListener);
  152.         c.removeMouseMotionListener(dragListener);
  153.         ((JMenu)c).removeChangeListener(menuChangeListener);
  154.         ((JMenu)c).removePropertyChangeListener(propertyChangeListener);
  155.     }
  156.  
  157.     protected void installDefaultIcons() {
  158.     // Icons
  159.     if ((menu != null) &&
  160.         (menu.getParent() != null) &&
  161.         !(menu.getParent() instanceof JMenuBar)) {
  162.         if (menuArrow == null ||
  163.         menuArrow instanceof UIResource) {
  164.         menuArrow = UIManager.getIcon("Menu.arrowIcon");
  165.         }
  166.         if (checkIcon == null ||
  167.         checkIcon instanceof UIResource) {
  168.         checkIcon = UIManager.getIcon("MenuItem.checkIcon");
  169.         }
  170.     }
  171.     }
  172.  
  173.     /**
  174.      * We draw the background in BasicGraphicsUtils.paintMenuItem()
  175.      * so override update (which fills the background of opaque
  176.      * components by default) to just call paint().
  177.      *
  178.      * @see BasicGraphicsUtils#paintMenuItem
  179.      */
  180.     public void update(Graphics g, JComponent c) {
  181.         paint(g, c);
  182.     }
  183.  
  184.     public void paint(Graphics g, JComponent c) {
  185.     installDefaultIcons();
  186.         ButtonModel bm = ((AbstractButton)c).getModel();
  187.     BasicGraphicsUtils.paintMenuItem(g, c, checkIcon, menuArrow,
  188.                      pressedBackground, pressedForeground,
  189.                      defaultTextIconGap);    
  190.     }
  191.  
  192.     public Dimension getMinimumSize(JComponent c) {
  193.         return getPreferredSize(c);
  194.     }
  195.  
  196.     public Dimension getMaximumSize(JComponent c) {
  197.         return getPreferredSize(c);
  198.     }
  199.  
  200.     public Insets getDefaultMargin(AbstractButton c) { 
  201.         return new Insets(2,2,2,2);
  202.     }
  203.  
  204.     public Dimension getPreferredSize(JComponent c) {
  205.     installDefaultIcons();
  206.     return BasicGraphicsUtils.getPreferredMenuItemSize(c,
  207.                                checkIcon, 
  208.                                menuArrow, 
  209.                                defaultTextIconGap);
  210.     }
  211.  
  212.  
  213.  
  214.  
  215.     public void processMouseEvent(JMenuItem item,MouseEvent e,MenuElement path[],MenuSelectionManager manager) {
  216.         Point p = e.getPoint();
  217.     if (item.isEnabled() == false)
  218.       return;
  219.  
  220.         if(p.x >= 0 && p.x < item.getWidth() &&
  221.            p.y >= 0 && p.y < item.getHeight()) {
  222.         JMenu menu = (JMenu)item;
  223.             MenuElement selectedPath[] = manager.getSelectedPath();
  224.             if(!(selectedPath.length > 0 && 
  225.          selectedPath[selectedPath.length-1] == 
  226.          menu.getPopupMenu())) {
  227.           if(menu.isTopLevelMenu() || 
  228.          menu.getDelay() == 0  ||
  229.          e.getID() == MouseEvent.MOUSE_DRAGGED) {
  230.         MenuElement newPath[] = new MenuElement[path.length+1];
  231.         System.arraycopy(path,0,newPath,0,path.length);
  232.         newPath[path.length] = menu.getPopupMenu();
  233.         manager.setSelectedPath(newPath);
  234.                 } else {
  235.                     manager.setSelectedPath(path);
  236.                     setupPostTimer(menu);
  237.                 }
  238.             }
  239.         } else if(e.getID() == MouseEvent.MOUSE_RELEASED) {
  240.         Component c = manager.componentForPoint(e.getComponent(), e.getPoint());
  241.         if (c == null)
  242.         manager.clearSelectedPath();
  243.         }
  244.     }
  245.  
  246.     private int lower(int ascii) {
  247.         if(ascii >= 'A' && ascii <= 'Z')
  248.             return ascii + 'a' - 'A';
  249.         else
  250.             return ascii;
  251.     }
  252.     
  253.     public void processKeyEvent(JMenuItem item,KeyEvent e,MenuElement path[],MenuSelectionManager manager) {
  254.         int key = item.getMnemonic();
  255.         if(key == 0)
  256.             return;
  257.         if(e.getID() == KeyEvent.KEY_PRESSED) {
  258.             if(lower(key) == lower((int)(e.getKeyChar()))) {
  259.                 JPopupMenu popupMenu = ((JMenu)item).getPopupMenu();
  260.                 MenuElement sub[] = popupMenu.getSubElements();
  261.                 if(sub.length > 0) {
  262.                     MenuElement newPath[] = new MenuElement[path.length + 2];
  263.                     System.arraycopy(path,0,newPath,0,path.length);
  264.                     newPath[path.length] = popupMenu;
  265.                     newPath[path.length+1] = sub[0];
  266.                     manager.setSelectedPath(newPath);
  267.                 }
  268.                 e.consume();
  269.             }
  270.         }
  271.     }
  272.     
  273.     protected void setupPostTimer(JMenu menu) {
  274.         Timer timer = new Timer(menu.getDelay(),new PostAction(menu,false));
  275.         timer.setRepeats(false);
  276.         timer.start();
  277.     }
  278.  
  279.     static class PostAction extends AbstractAction {
  280.     JMenu menu;
  281.         boolean force=false;
  282.  
  283.         PostAction(JMenu menu,boolean shouldForce) {
  284.         super("postAction");
  285.         this.menu = menu;
  286.             this.force = shouldForce;
  287.     }
  288.     
  289.     public void actionPerformed(ActionEvent e) {
  290.             MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager();
  291.             if(force) {
  292.                 Container cnt = menu.getParent();
  293.                 if(cnt != null && cnt instanceof JMenuBar) {
  294.                     MenuElement me[];
  295.                     MenuElement subElements[];
  296.  
  297.                     subElements = menu.getPopupMenu().getSubElements();
  298.                     if(subElements.length > 0) {
  299.                         me = new MenuElement[4];
  300.                         me[0] = (MenuElement) cnt;
  301.                         me[1] = (MenuElement) menu;
  302.                         me[2] = (MenuElement) menu.getPopupMenu();
  303.                         me[3] = subElements[0];
  304.                         defaultManager.setSelectedPath(me);
  305.                     } else {
  306.                         me = new MenuElement[2];
  307.                         me[0] = (MenuElement)cnt;
  308.                         me[1] = menu;
  309.                         defaultManager.setSelectedPath(me);
  310.                     }
  311.                 }
  312.             } else {
  313.                 MenuElement path[] = defaultManager.getSelectedPath();
  314.                 if(path.length > 0 && path[path.length-1] == menu) {
  315.                     MenuElement newPath[] = new MenuElement[path.length+1];
  316.                     System.arraycopy(path,0,newPath,0,path.length);
  317.                     newPath[path.length] = menu.getPopupMenu();
  318.                     MenuSelectionManager.defaultManager().setSelectedPath(newPath);
  319.                 }
  320.             }
  321.         }
  322.  
  323.     public boolean isEnabled() {
  324.         return menu.getModel().isEnabled();
  325.     }
  326.     }
  327.  
  328.  
  329.     class MenuPropertyChangeListener implements PropertyChangeListener, Serializable {
  330.         public void propertyChange(PropertyChangeEvent e) {
  331.         String prop = e.getPropertyName();
  332.         if(prop.equals(AbstractButton.MNEMONIC_CHANGED_PROPERTY)) {
  333.         validateKeyboardAccelerator();            
  334.         }
  335.     }
  336.     }
  337.  
  338.     static class MouseListener extends MouseAdapter implements Serializable {
  339.         JMenu menu;
  340.         public MouseListener(JMenu m) {
  341.             menu = m;
  342.         }
  343.  
  344.         public void mousePressed(MouseEvent e) {
  345.             if(menu.isTopLevelMenu() && menu.isEnabled()) {
  346.                 Point p = e.getPoint();
  347.                 if(p.x >= 0 && p.x < menu.getWidth() &&
  348.                    p.y >= 0 && p.y < menu.getHeight()) {
  349.                     if(menu.isSelected()) {
  350.                         MenuSelectionManager.defaultManager().clearSelectedPath();
  351.                     } else {
  352.                         Container cnt = menu.getParent();
  353.                         if(cnt != null && cnt instanceof JMenuBar) {
  354.                             MenuElement me[] = new MenuElement[2];
  355.                             me[0]=(MenuElement)cnt;
  356.                             me[1]=menu;
  357.                             MenuSelectionManager.defaultManager().setSelectedPath(me);
  358.                         }
  359.                     }
  360.                 }
  361.             }
  362.             MenuSelectionManager.defaultManager().processMouseEvent(e);
  363.         }
  364.  
  365.         public void mouseReleased(MouseEvent e) {
  366.             MenuSelectionManager.defaultManager().processMouseEvent(e);
  367.         }
  368.         public void mouseEntered(MouseEvent e) {
  369.             MenuSelectionManager.defaultManager().processMouseEvent(e);
  370.         }
  371.         public void mouseExited(MouseEvent e) {
  372.             MenuSelectionManager.defaultManager().processMouseEvent(e);
  373.         }
  374.     }
  375.  
  376.     public void menuCanceled(JMenu m) {
  377.         MenuSelectionManager manager = MenuSelectionManager.defaultManager();
  378.         if(manager.isComponentPartOfCurrentMenu(m))
  379.             MenuSelectionManager.defaultManager().clearSelectedPath();
  380.     }
  381.  
  382.     static class MenuChangeListener implements ChangeListener, Serializable {
  383.         JMenu    menu;
  384.     BasicMenuUI ui;
  385.         boolean  isSelected = false;
  386.         Component wasFocused;
  387.  
  388.         public MenuChangeListener(JMenu m, BasicMenuUI ui) {
  389.             menu = m;
  390.             this.ui = ui;
  391.             validateKeyboardActions(menu.isSelected());
  392.         }
  393.  
  394.         public void stateChanged(ChangeEvent e) {
  395.             validateKeyboardActions(menu.isSelected());
  396.         }
  397.  
  398.         Component findFocusedComponent(Component c) {
  399.             Container parent;
  400.             for(parent = c.getParent() ; parent != null ; parent = parent.getParent()) {
  401.                 if(parent instanceof java.awt.Window)
  402.                     return ((java.awt.Window)parent).getFocusOwner();
  403.             }
  404.             return null;
  405.         }
  406.  
  407.         void validateKeyboardActions(boolean sel) {
  408.             if(sel != isSelected) {
  409.                 isSelected = sel;
  410.                 if(isSelected) {
  411.                     boolean isRequestFocusEnabled = menu.isRequestFocusEnabled();
  412.                     wasFocused = findFocusedComponent(menu);
  413.                     if(!isRequestFocusEnabled)
  414.                         menu.setRequestFocusEnabled(true);
  415.                     menu.requestFocus();
  416.                     if(!isRequestFocusEnabled)
  417.                         menu.setRequestFocusEnabled(false);
  418.                     menu.registerKeyboardAction(new CancelAction(),
  419.                                                 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0,false),
  420.                                                 JComponent.WHEN_IN_FOCUSED_WINDOW);
  421.                     menu.registerKeyboardAction(new SelectNextItemAction(),
  422.                                                 KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0,false),
  423.                                                 JComponent.WHEN_IN_FOCUSED_WINDOW);
  424.                     menu.registerKeyboardAction(new SelectPreviousItemAction(),
  425.                                                 KeyStroke.getKeyStroke(KeyEvent.VK_UP,0,false),
  426.                                                 JComponent.WHEN_IN_FOCUSED_WINDOW);
  427.                     menu.registerKeyboardAction(new SelectParentItemAction(),
  428.                                                 KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0,false),
  429.                                                 JComponent.WHEN_IN_FOCUSED_WINDOW);
  430.                     menu.registerKeyboardAction(new SelectChildItemAction(),
  431.                                                 KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0,false),
  432.                                                 JComponent.WHEN_IN_FOCUSED_WINDOW);
  433.                     menu.registerKeyboardAction(new ReturnAction(),
  434.                                                 KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false),
  435.                                                 JComponent.WHEN_IN_FOCUSED_WINDOW);
  436.                 } else {
  437.                     menu.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0,false));
  438.                     menu.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0,false));
  439.                     menu.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0,false));
  440.                     menu.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0,false));
  441.                     menu.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0,false));
  442.                     menu.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false));
  443.                     if(wasFocused != null) {
  444.                         if(wasFocused instanceof JComponent) {
  445.                             JComponent jc = (JComponent) wasFocused;
  446.                             boolean isRFEnabled = jc.isRequestFocusEnabled();
  447.                             if(!isRFEnabled)
  448.                                 jc.setRequestFocusEnabled(true);
  449.                             wasFocused.requestFocus();
  450.                             if(!isRFEnabled)
  451.                                 jc.setRequestFocusEnabled(false);
  452.                         } else
  453.                             wasFocused.requestFocus();
  454.                         wasFocused = null;
  455.                     }
  456.                 }
  457.             }
  458.         }
  459.  
  460.         private class CancelAction extends AbstractAction {
  461.             public void actionPerformed(ActionEvent e) {
  462.                 MenuElement path[] = MenuSelectionManager.defaultManager().getSelectedPath();
  463.                 if(path.length > 4) { /* PENDING(arnaud) Change this to 2 when a mouse grabber is available for MenuBar */
  464.                     MenuElement newPath[] = new MenuElement[path.length - 2];
  465.                     System.arraycopy(path,0,newPath,0,path.length-2);
  466.                     MenuSelectionManager.defaultManager().setSelectedPath(newPath);
  467.                 } else
  468.                     MenuSelectionManager.defaultManager().clearSelectedPath();
  469.             }
  470.         }
  471.  
  472.         private class ReturnAction extends AbstractAction {
  473.             public void actionPerformed(ActionEvent e) {
  474.                 MenuElement path[] = MenuSelectionManager.defaultManager().getSelectedPath();
  475.                 MenuElement lastElement;
  476.                 if(path.length > 0) {
  477.                     lastElement = path[path.length-1];
  478.                     if(lastElement instanceof JMenu) {
  479.                         MenuElement newPath[] = new MenuElement[path.length+1];
  480.                         System.arraycopy(path,0,newPath,0,path.length);
  481.                         newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
  482.                         MenuSelectionManager.defaultManager().setSelectedPath(newPath);
  483.                     } else if(lastElement instanceof JMenuItem) {
  484.                         MenuSelectionManager.defaultManager().clearSelectedPath();
  485.                         ((JMenuItem)lastElement).doClick(0);
  486.                         ((JMenuItem)lastElement).setArmed(false);
  487.                     }
  488.                 }
  489.             }
  490.         }
  491.  
  492.         private MenuElement nextEnabledChild(MenuElement e[],int fromIndex) {
  493.             int i,c;
  494.             for(i=fromIndex,c=e.length ; i < c ; i++) {
  495.         if (e[i]!=null) {
  496.             Component comp = e[i].getComponent();
  497.             if(comp != null && comp.isEnabled())
  498.             return e[i];
  499.         }
  500.         }
  501.             return null;
  502.         }
  503.  
  504.         private MenuElement previousEnabledChild(MenuElement e[],int fromIndex) {
  505.             int i;
  506.             for(i=fromIndex ; i >= 0 ; i--) {
  507.         if (e[i]!=null) {
  508.             Component comp = e[i].getComponent();
  509.             if(comp != null && comp.isEnabled())
  510.             return e[i];
  511.         }
  512.         }
  513.             return null;
  514.         }
  515.  
  516.         private class SelectNextItemAction extends AbstractAction {
  517.             public void actionPerformed(ActionEvent e) {
  518.                 MenuElement currentSelection[] = MenuSelectionManager.defaultManager().getSelectedPath();
  519.                 if(currentSelection.length > 1) {
  520.                     MenuElement parent = currentSelection[currentSelection.length-2];
  521.                     if(parent.getComponent() instanceof JMenu) {
  522.                         MenuElement childs[];
  523.                         parent = currentSelection[currentSelection.length-1];
  524.                         childs = parent.getSubElements();
  525.                         if(childs.length > 0) {
  526.                             MenuElement newPath[] = new MenuElement[currentSelection.length+1];
  527.                             System.arraycopy(currentSelection,0,
  528.                                              newPath,0,currentSelection.length);
  529.                             newPath[currentSelection.length] = nextEnabledChild(childs,0);
  530.                             if(newPath[currentSelection.length] != null)
  531.                                 MenuSelectionManager.defaultManager().setSelectedPath(newPath);
  532.                         }
  533.                     } else {
  534.                         MenuElement childs[] = parent.getSubElements();
  535.                         MenuElement nextChild;
  536.                         int i,c;
  537.                         for(i=0,c=childs.length;i<c;i++) {
  538.                             if(childs[i] == currentSelection[currentSelection.length-1]) {
  539.                                 nextChild = nextEnabledChild(childs,i+1);
  540.                                 if(nextChild == null)
  541.                                     nextChild = nextEnabledChild(childs,0);
  542.                                 if(nextChild != null) {
  543.                                     currentSelection[currentSelection.length-1] = nextChild;
  544.                                     MenuSelectionManager.defaultManager().setSelectedPath(currentSelection);
  545.                                 }
  546.                                 break;
  547.                             }
  548.                         }
  549.                     }
  550.                 }
  551.             }
  552.         }
  553.  
  554.         private class SelectPreviousItemAction extends AbstractAction {
  555.             public void actionPerformed(ActionEvent e) {
  556.                 MenuElement currentSelection[] = MenuSelectionManager.defaultManager().getSelectedPath();
  557.                 if(currentSelection.length > 1) {
  558.                     MenuElement parent = currentSelection[currentSelection.length-2];
  559.                     if(parent.getComponent() instanceof JMenu) {
  560.                         MenuElement childs[];
  561.                         parent = currentSelection[currentSelection.length-1];
  562.                         childs = parent.getSubElements();
  563.                         if(childs.length > 0) {
  564.                             MenuElement newPath[] = new MenuElement[currentSelection.length+1];
  565.                             System.arraycopy(currentSelection,0,
  566.                                              newPath,0,currentSelection.length);
  567.                             newPath[currentSelection.length] = previousEnabledChild(childs,childs.length-1);
  568.                             if(newPath[currentSelection.length] != null)
  569.                                 MenuSelectionManager.defaultManager().setSelectedPath(newPath);
  570.                         }
  571.                     } else {
  572.                         MenuElement childs[] = parent.getSubElements();
  573.                         MenuElement nextChild;
  574.                         int i,c;
  575.                         for(i=0,c=childs.length;i<c;i++) {
  576.                             if(childs[i] == currentSelection[currentSelection.length-1]) {
  577.                                 nextChild = previousEnabledChild(childs,i-1);
  578.                                 if(nextChild == null)
  579.                                     nextChild = previousEnabledChild(childs,childs.length-1);
  580.                                 if(nextChild != null) {
  581.                                     currentSelection[currentSelection.length-1] = nextChild;
  582.                                     MenuSelectionManager.defaultManager().setSelectedPath(currentSelection);
  583.                                 }
  584.                                 break;
  585.                             }
  586.                         }
  587.                     }
  588.                 }
  589.             }
  590.         }
  591.  
  592.         private class SelectParentItemAction extends AbstractAction {
  593.             public void actionPerformed(ActionEvent e) {
  594.                 MenuElement path[] = MenuSelectionManager.defaultManager().getSelectedPath();
  595.                 
  596.                 if(path.length > 3 && path[path.length-3].getComponent() instanceof JMenu &&
  597.                    !((JMenu)path[path.length-3].getComponent()).isTopLevelMenu()) {
  598.                     MenuElement newPath[] = new MenuElement[path.length-2];
  599.                     System.arraycopy(path,0,newPath,0,path.length-2);
  600.                     MenuSelectionManager.defaultManager().setSelectedPath(newPath);
  601.                 } else if(path.length > 0 && path[0].getComponent() instanceof JMenuBar) {
  602.                     MenuElement nextMenu=null,popup=null,firstItem=null;
  603.                     MenuElement tmp[];
  604.                     int i,c;
  605.                     
  606.                     if(path.length > 1) {
  607.                         MenuElement previousElement;
  608.                         tmp = path[0].getSubElements();
  609.                         for(i=0,c=tmp.length;i<c;i++) {
  610.                             if(tmp[i] == path[1]) {
  611.                                 nextMenu = previousEnabledChild(tmp,i-1);
  612.                                 if(nextMenu == null)
  613.                                     nextMenu = previousEnabledChild(tmp,tmp.length-1);
  614.                             }
  615.                         }
  616.                     }
  617.                     
  618.                     if(nextMenu != null) {
  619.                         MenuElement newSelection[];
  620.                         popup = ((JMenu)nextMenu).getPopupMenu();
  621.                         if(((JMenu)nextMenu).isTopLevelMenu()) 
  622.                             firstItem = null;
  623.                         else {
  624.                             tmp = popup.getSubElements();
  625.                             if(tmp.length > 0) 
  626.                                 firstItem = nextEnabledChild(tmp,0);
  627.                         }
  628.  
  629.                         if(firstItem != null) {
  630.                             newSelection = new MenuElement[4];
  631.                             newSelection[0] = path[0];
  632.                             newSelection[1] = nextMenu;
  633.                             newSelection[2] = popup;
  634.                             newSelection[3] = firstItem;
  635.                         } else {
  636.                             newSelection = new MenuElement[3];
  637.                             newSelection[0] = path[0];
  638.                             newSelection[1] = nextMenu;
  639.                             newSelection[2] = popup;
  640.                         }
  641.                         MenuSelectionManager.defaultManager().setSelectedPath(newSelection);
  642.                     }
  643.                 }
  644.             }
  645.         }
  646.  
  647.         private class SelectChildItemAction extends AbstractAction {
  648.             public void actionPerformed(ActionEvent e) {
  649.                 MenuElement path[] = MenuSelectionManager.defaultManager().getSelectedPath();
  650.                 
  651.                 if(path.length > 0 && path[path.length-1].getComponent().isEnabled() && 
  652.                    path[path.length-1].getComponent() instanceof JMenu &&
  653.                    !((JMenu)path[path.length-1].getComponent()).isTopLevelMenu()) {
  654.                     MenuElement newPath[] = new MenuElement[path.length+2];
  655.                     MenuElement subElements[];
  656.                     System.arraycopy(path,0,newPath,0,path.length);
  657.                     newPath[path.length] = ((JMenu)path[path.length-1].getComponent()).getPopupMenu();
  658.                     subElements = newPath[path.length].getSubElements();
  659.                     if(subElements.length > 0) {
  660.                         newPath[path.length+1] = nextEnabledChild(subElements,0);
  661.                         MenuSelectionManager.defaultManager().setSelectedPath(newPath);
  662.                     }
  663.                 } else if(path.length > 0 && path[0].getComponent() instanceof JMenuBar) {
  664.                     MenuElement nextMenu=null,popup=null,firstItem=null;
  665.                     MenuElement tmp[];
  666.                     int i,c;
  667.                     
  668.                     if(path.length > 1) {
  669.                         tmp = path[0].getSubElements();
  670.                         for(i=0,c=tmp.length;i<c;i++) {
  671.                             if(tmp[i] == path[1]) {
  672.                                 nextMenu = nextEnabledChild(tmp,i+1);
  673.                                 if(nextMenu == null)
  674.                                     nextMenu = nextEnabledChild(tmp,0);
  675.                             }
  676.                         }
  677.                     }
  678.                     
  679.                     if(nextMenu != null) {
  680.                         MenuElement newSelection[];
  681.                         popup = ((JMenu)nextMenu).getPopupMenu();
  682.                         if(((JMenu)nextMenu).isTopLevelMenu()) 
  683.                             firstItem = null;
  684.                         else {
  685.                             tmp = popup.getSubElements();
  686.                             if(tmp.length > 0) 
  687.                                 firstItem = nextEnabledChild(tmp,0);
  688.                         }
  689.  
  690.                         if(firstItem != null) {
  691.                             newSelection = new MenuElement[4];
  692.                             newSelection[0] = path[0];
  693.                             newSelection[1] = nextMenu;
  694.                             newSelection[2] = popup;
  695.                             newSelection[3] = firstItem;
  696.                         } else {
  697.                             newSelection = new MenuElement[3];
  698.                             newSelection[0] = path[0];
  699.                             newSelection[1] = nextMenu;
  700.                             newSelection[2] = popup;
  701.                         }
  702.                         MenuSelectionManager.defaultManager().setSelectedPath(newSelection);
  703.                     }
  704.                 }
  705.             }
  706.         }
  707.     }
  708. }
  709.  
  710.  
  711.  
  712.  
  713.